package cn.edu.xjtu.nbrash.xbarsuite.floatbutton;

import cn.edu.xjtu.nbrash.xbarsuite.RashIntents;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.IBinder;
import android.util.Log;

/**
 * StickyService.java
 *
 * @author Jingkai Tang jingkaitang@gmail.com
 * @version 0.0.1 Nov 14, 2014 6:50:06 PM
 *
 */
public class StickyService extends Service {

	private static final String TAG = StickyService.class.getName();

	public StickyService() {
	}

	@Override
	public IBinder onBind(Intent intent) {
		return null;
	}

	@Override
	public int onStartCommand(Intent intent, int flags, int startId) {
		Log.e(TAG, "Start");
		flags = START_STICKY;
		FloatButtonService.startActionFloatButton(this.getApplicationContext());
		return super.onStartCommand(intent, flags, startId);
	}

	@Override
	public void onDestroy() {
		Log.e(TAG, "Destroy");
		SharedPreferences sharePreferences = getSharedPreferences(
				RashIntents.FloatButton.FLOAT_BUTTON_PREFERENCES,
				Context.MODE_PRIVATE);
		FloatButton floatButton = FloatButtonCreator
				.getFloatButton(RashIntents.FloatButton.FLOAT_BUTTON_FLAG);
		sharePreferences
				.edit()
				.putInt(RashIntents.FloatButton.FLOAT_BUTTON_POSITION_X,
						floatButton.getLayoutParams().x)
				.putInt(RashIntents.FloatButton.FLOAT_BUTTON_POSITION_Y,
						floatButton.getLayoutParams().y)
				.putFloat(RashIntents.FloatButton.FLOAT_BUTTON_ALPHA,
						floatButton.getLayoutParams().alpha).commit();
		super.onDestroy();
		Intent intent = new Intent(RashIntents.FloatButton.FLOAT_BUTTON_KILLED);
		sendBroadcast(intent);
	}

}
